package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.pojo.CategorySecond;
import net.zjitc.service.CategorySecondService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.impl.CategorySecondServiceImpl;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by Administrator on 2017/11/22.
 */
public class AdminCategorySecond {
    CategorySecondService secondService = new CategorySecondServiceImpl();
    CategoryService categoryService = new CategoryServiceImpl();

    //显示列表
    @URLMapping(url = "/AdminCategorySecondFindALL")
    public String findAll(HttpServletRequest request, HttpServletResponse response){
        List<CategorySecond> list = secondService.findAll();
        request.setAttribute("cslist",list);
        return "/admin/categorysecond/list.jsp" ;
    }

    //显示添加页面
    @URLMapping(url = "/AdminCategorySecondAddUI")
    public String addUI(HttpServletRequest request, HttpServletResponse response){
        //准备一级分类的回显数据
        List<Category> clist = categoryService.findAll();
        request.setAttribute("clist",clist);
        return "/admin/categorysecond/add.jsp" ;
    }

    //添加功能
    @URLMapping(url = "/AdminCategorySecondAdd")
    public String add(HttpServletRequest request, HttpServletResponse response){
        //csname  cid
        Category category = new Category();
        CategorySecond categorySecond = new CategorySecond();
        try {
            BeanUtils.populate(category,request.getParameterMap());
            BeanUtils.populate(categorySecond,request.getParameterMap());
            categorySecond.setCategory(category);
            int count = secondService.insert(categorySecond);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //添加完后，要重定向到。。。。。
        String url = request.getContextPath()+"/AdminCategorySecondFindALL";
        return "Redirect:"+url ;
    }

    //显示修改页面
    @URLMapping(url = "/AdminCategorySecondEditUI")
    public String editUI(HttpServletRequest request, HttpServletResponse response){
        String csid = request.getParameter("csid");
        CategorySecond categorySecond = secondService.findByCSId(csid);
        request.setAttribute("cs",categorySecond);
        List<Category> list = categoryService.findAll();
        request.setAttribute("clist",list);
        return "/admin/categorysecond/edit.jsp" ;
    }


    //更新功能
    @URLMapping(url = "/AdminCategorySecondEdit")
    public String edit(HttpServletRequest request, HttpServletResponse response){
        Category category = new Category();
        CategorySecond categorySecond = new CategorySecond();
        try {
            BeanUtils.populate(category,request.getParameterMap());
            BeanUtils.populate(categorySecond,request.getParameterMap());
            categorySecond.setCategory(category);
            int count = secondService.update(categorySecond);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //更新完后，要重定向到。。。。。
        String url = request.getContextPath()+"/AdminCategorySecondFindALL";
        return "Redirect:"+url ;
    }

    //删除功能
    @URLMapping(url = "/AdminCategorySecondDelete")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        String csid = request.getParameter("csid");
        int count = secondService.delete(csid);
        //删除完后，要重定向到。。。。。
        String url = request.getContextPath()+"/AdminCategorySecondFindALL";
        return "Redirect:"+url ;
    }
}
